// pages/mealdetails/index.js
const app = getApp()  // 引入app
const _request = require("../../utils/request.js"); // 引入require;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    marginBottom: app.globalData.marginBottom,
    imgUrl: app.globalData.imgUrl, //全局img路径
    showstutas: false,
    mealObj: null,
    timelist:[],
    showLength:4,
    show: false
  },


  // 展示更多
  showmore(){
    this.setData({
      showLength: this.data.timelist.length
    })
  },

  // 展示5条
  showmuch(){
    this.setData({
      showLength: 4
    })
  },

  // 跳转理发师详情
  toHair(e){
    const {
      item
    } = e.currentTarget.dataset
    let obj
    _request.postObj('/stylistCard/card', {
      stylistId: item.stylistId,
      userId: wx.getStorageSync('userid')
    }).then((res) => {
      obj = res.data.data
      let newobj = JSON.stringify(obj)
      wx.navigateTo({
        url: '/pages/hairstylistdetails/index?hair=' + encodeURIComponent(newobj),
      })
    }).catch((error) => {
      console.log(error, 'error');
    })
  },

  // 退款
  onRefund(e){
    const {
      id
    } = e.currentTarget.dataset
    wx.showModal({
      content: '点击确认本套餐劵将失效，购买金额将原路返回。请确认是否要退款',
      title: '',
      success: (result) => {
        console.log(result);
        if(result.confirm){
          _request.get('/userCoupon/refundPackageUse', {
            id: id,
            userId: wx.getStorageSync('userid')
          }).then((res) => {
            if(res.data.code == 200){
              console.log(res.data.code);
              wx.showToast({
                title: '套餐劵已退款，金额将在24小时内到账。请注意查收',
                icon: 'none',
                duration: 2000
              })
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1,
                })
              }, 1000);
            }
          }).catch((error) => {
            console.log(error, 'error');
          })
        }else if(result.cancel){
        }
      },
      fail: (res) => {},
      complete: (res) => {},
    })
  },

  showPopup() {
    this.setData({ show: true });
  },

  onClose() {
    this.setData({ show: false });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let data = JSON.parse(decodeURIComponent(options.data))
    this.setData({
      mealObj: data
    })
    console.log(data);
    _request.get('/userCoupon/getPackageUseByUserIdAndPackageId', {
      id: data.id,
      userId: wx.getStorageSync('userid')
    }).then((res) => {
      this.setData({
        timelist: res.data.data
      })
    }).catch((error) => {
      console.log(error, 'error');
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})